package com.tanhua.controller;

import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.SmallVideoService;
import com.tanhua.server.UserFreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {
    @Autowired
    private SmallVideoService smallVideoService;
    @Autowired
    private UserFreezeService userFreezeService;
    /**
     * 发布视频
     * @param videoThumbnail 视频封面图片
     * @param videoFile      视频
     * @return Void
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity<Void> saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        userFreezeService.checkUserStatus("3", UserHolder.getUserId(), "您已被禁止发布视频，详情请联系管理员");
        smallVideoService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = smallVideoService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 关注视频作者
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void> userFocus(@PathVariable("id") Long followUserId) {
        smallVideoService.userFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注视频作者
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void> userUnFocus(@PathVariable("id") Long followUserId) {
        smallVideoService.userUnFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 喜欢
     *
     * @param id 视频Id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<PageResult> like(@PathVariable(value = "id") String id) {
        smallVideoService.like(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 不喜欢
     *
     * @param id 视频Id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<PageResult> dislike(@PathVariable(value = "id") String id) {
        smallVideoService.dislike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 发布评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> addComments(@RequestBody Map map) {
        String comment = map.get("comment").toString();
        String id = map.get("id").toString();
        smallVideoService.addComments(id, comment);
        return null;
    }
//查询评论
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> addComments(@PathVariable(value = "id") String id,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = smallVideoService.getComments(id, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    //评论点赞,id 评论id
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Void> commentsLike(@PathVariable String id) {
        smallVideoService.likeComments(UserHolder.getUserId(),id);
        return ResponseEntity.ok(null);
    }

    //评论点赞,id 评论id
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Void> commentsDisLike(@PathVariable String id) {
        smallVideoService.dislikeComments(UserHolder.getUserId(),id);
        return ResponseEntity.ok(null);
    }


}
